package com.meshnetics.gb.util.formatters;

import com.meshnetics.gb.util.Conversions;

/**
 * Hex byte formatter. Converts byte to hex string. By default has no prefix (equals constructor
 * with 'false' parameter). For example: 00, 01, 10, 7F, 80, FF or 0x00, 0x01, 0x10, 0x7F, 0x80,
 * 0xFF.
 *
 * @author Yury Aserchev (yaserchev@meshnetics.com)
 *         Date: 30.07.2007 13:05:00
 */
final class ByteFormatterHex implements ByteFormatter{
    private final boolean addPrefix;

    public ByteFormatterHex(final boolean addPrefix){
        this.addPrefix = addPrefix;
    }

    public ByteFormatterHex(){
        this(false);
    }

    public String format(final byte b) {
        final StringBuffer buff = new StringBuffer(Integer.toHexString(Conversions.expandByteToInt(b)).toUpperCase());
        if (buff.length() < 2) {
            buff.insert(0, "0");
        }
        if (addPrefix){
            buff.insert(0, Conversions.HEX_PREFIX);
        }
        return buff.toString();
    }

    boolean isAddPrefix() {
        return addPrefix;
    }
}
